﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System.Text;
using System.Text.RegularExpressions;

#endregion

namespace ZO.SmartCore.My4CSharp
{
    internal class QuoteDelimitedFieldBuilder
    {

        #region Constructors
        public QuoteDelimitedFieldBuilder(Regex DelimiterRegex, string SpaceChars)
        {
            this.m_Field = new StringBuilder();
            this.m_DelimiterRegex = DelimiterRegex;
            this.m_SpaceChars = SpaceChars;
        }

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private int m_DelimiterLength;
        private Regex m_DelimiterRegex;
        private StringBuilder m_Field;
        private bool m_FieldFinished;
        private int m_Index;
        private bool m_MalformedLine;
        private string m_SpaceChars;
        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        public int DelimiterLength
        {
            get
            {
                return this.m_DelimiterLength;
            }
        }

        public string Field
        {
            get
            {
                return this.m_Field.ToString();
            }
        }

        public bool FieldFinished
        {
            get
            {
                return this.m_FieldFinished;
            }
        }

        public int Index
        {
            get
            {
                return this.m_Index;
            }
        }

        public bool MalformedLine
        {
            get
            {
                return this.m_MalformedLine;
            }
        }
        #endregion

        #region Methods
        public void BuildField(string Line, int StartAt)
        {
            this.m_Index = StartAt;
            int num1 = Line.Length;
            while (this.m_Index < num1)
            {
                if (Line[this.m_Index] == '"')
                {
                    int num2;
                    if ((this.m_Index + 1) == num1)
                    {
                        this.m_FieldFinished = true;
                        this.m_DelimiterLength = 1;
                        this.m_Index++;
                        return;
                    }
                    if (((this.m_Index + 1) < Line.Length) & (Line[this.m_Index + 1] == '"'))
                    {
                        this.m_Field.Append('"');
                        this.m_Index += 2;
                        continue;
                    }
                    Match match1 = this.m_DelimiterRegex.Match(Line, this.m_Index + 1);
                    if (!match1.Success)
                    {
                        num2 = num1 - 1;
                    }
                    else
                    {
                        num2 = match1.Index - 1;
                    }
                    int num4 = num2;
                    for (int num3 = this.m_Index + 1; num3 <= num4; num3++)
                    {
                        if (this.m_SpaceChars.IndexOf(Line[num3]) < 0)
                        {
                            this.m_MalformedLine = true;
                            return;
                        }
                    }
                    this.m_DelimiterLength = (1 + num2) - this.m_Index;
                    if (match1.Success)
                    {
                        this.m_DelimiterLength += match1.Length;
                    }
                    this.m_FieldFinished = true;
                    return;
                }
                this.m_Field.Append(Line[this.m_Index]);
                this.m_Index++;
            }
        }
        #endregion
    }
}
